package de.hauck.rtmidlet.model;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;



import de.hauck.rtmidlet.utils.MyLogger;
import de.hauck.rtmidlet.utils.Utils;

public class YahooBean {
	private String Name, Date, Time, Open, High, Low, Volume;
	private double Kurs, ChangeAbs, ChangeProc;

	public String getName() {
		return Name;
	}

	public void setName(String name) {
		Name = name;
	}

	public double getKurs() {
		return Kurs;
	}

	public void setKurs(double kurs) {
		Kurs = kurs;
	}

	public String getDate() {
		return Date;
	}

	public void setDate(String date) {
		Date = date;
	}

	public String getTime() {
		return Time;
	}

	public void setTime(String time) {
		Time = time;
	}

	public double getChangeAbs() {
		return ChangeAbs;
	}

	public double getChangeProc() {
		return ChangeProc;
	}

	public void setChangeAbs(double changeAbs) {
		ChangeAbs = changeAbs;
	}

	public void setChangeProc(double changeProc) {
		ChangeProc = changeProc;
	}

	public String getOpen() {
		return Open;
	}

	public void setOpen(String open) {
		Open = open;
	}

	public String getHigh() {
		return High;
	}

	public void setHigh(String high) {
		High = high;
	}

	public String getLow() {
		return Low;
	}

	public void setLow(String low) {
		Low = low;
	}

	public String getVolume() {
		return Volume;
	}

	public void setVolume(String volume) {
		Volume = volume;
	}

	public void setToken(int i, String token) {

		switch (i) {
		case 0: {
			setName(token);
			break;
		}
		case 1: {
			double l = 0;
			try {
				l = Double.parseDouble(token);

			} catch (NumberFormatException ex) {
				MyLogger.error(ex.getMessage());
				// ex.printStackTrace();
			} finally {
				setKurs(l);
			}
			break;
		}
		case 2: {
			setDate(token);
			break;
		}
		case 3: {
			setTime(token);
			break;
		}
		case 4: {
			double l = 0;
			try {
				l = Double.parseDouble(token);
			} catch (NumberFormatException ex) {
				MyLogger.error(ex.getMessage());
				// ex.printStackTrace();
			} finally {
				setChangeAbs(l);
			}
			break;
		}
		case 5: {
			double l = 0;
			try {
				l = Double.parseDouble(token.substring(1, token.length() - 2));
			} catch (NumberFormatException ex) {
				MyLogger.error(ex.getMessage());
				// ex.printStackTrace();
			} finally {
				setChangeProc(l);
			}
			break;
		}
		case 6: {
			setOpen(token);
			break;
		}
		case 7: {
			setHigh(token);
			break;
		}
		case 8: {
			setLow(token);
			break;
		}
		case 9: {
			setVolume(token);
			break;
		}
		}

	}

	public void printBean() {
		System.out.println("Bean: [\"" + Name + "\", " + "\"" + Kurs + "\", "
				+ "\"" + Date + "\", " + "\"" + Time + "\", " + "\""
				+ ChangeAbs + "\", " + "\"" + ChangeProc + "\", " + "\"" + Open
				+ "\", " + "\"" + High + "\", " + "\"" + Low + "\", " + "\""
				+ Volume + "\"]");
	}

	public String getCurentTime() {
		Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Etc/GMT+5"));
		
		try {
			String date = Utils.replace(getDate(), "\"", "");
			if (date.indexOf(47) != date.lastIndexOf(47)) {
				// at least two "/" in date-String
				String month = date.substring(0, date.indexOf(47));
				String day = date.substring(date.indexOf(47) + 1, date
						.lastIndexOf(47));
				String year = date.substring(date.lastIndexOf(47) + 1, date
						.length());
				cal.set(Calendar.YEAR, Integer.valueOf(year).intValue());
				cal.set(Calendar.MONTH, Integer.valueOf(month).intValue() - 1);
				cal.set(Calendar.DAY_OF_MONTH, Integer.valueOf(day).intValue());
			}
			String time = Utils.replace(getTime(), "\"", "");
			if (time.indexOf(":") != -1) {
				String hour = time.substring(0, time.indexOf(":"));
				String minute = time.substring(time.indexOf(":") + 1, time
						.indexOf(":") + 3);
				String xm = time.substring(time.length() - 2, time.length());
				cal.set(Calendar.HOUR, Integer.valueOf(hour).intValue());
				cal.set(Calendar.MINUTE, Integer.valueOf(minute).intValue());
				if (xm.equals("am"))
					cal.set(Calendar.AM_PM, Calendar.AM);
				else
					cal.set(Calendar.AM_PM, Calendar.PM);
			}
		} catch (Exception ex) {
			MyLogger.error("Error parsing date/times");
		}
		// System.out.println(Utils.convertCal2YahooDateTimeStamp(cal));
		return Utils.convertCal2YahooDateTimeStamp(cal);

	}
}
